/**   
* @Title: UsbkeyAction.java
* @Package gov.pbc.gx.csm.usbkey.view
* @Description: TODO(用一句话描述该文件做什么)
* @author WLF
* @date 2015-12-23 下午10:28:24
* @version V1.0
*/ 
package gov.pbc.nn.kjgl.maintenance.usb.view;

import gov.pbc.nn.kjgl.maintenance.usb.dao.UsbkeyDao;
import gov.pbc.nn.kjgl.maintenance.usb.entity.CommonUserUsbkey;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

/** 
 * @ClassName: UsbkeyAction
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author WLF
 * @date 2015-12-23 下午10:28:24
 * 
 */
@Component(UsbkeyAction.bean_id)
public class UsbkeyAction {
	public final static String bean_id="usbkeyAction";
	@Resource(name=UsbkeyDao.bean_id)
	private UsbkeyDao usbkeyDao;
	
	@DataResolver
	@Transactional
	public void saveUk(List<CommonUserUsbkey> list){
		try {
			
			for(CommonUserUsbkey uk:list){
				EntityState state = EntityUtils.getState(uk);
				if (EntityState.NEW.equals(state)){
					uk.setBindingDate(new Date());
					usbkeyDao.saveUk(uk);
				}else if (EntityState.MODIFIED.equals(state)){
					usbkeyDao.updateUk(uk);
				}else if (EntityState.DELETED.equals(state)){
					usbkeyDao.deleteUk(uk);
				}
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@DataProvider
	public void getAllUkPage(Page<CommonUserUsbkey> page,Criteria criteria) throws Exception{
		usbkeyDao.getAllUkPage(page, criteria);
	}
	
	@Expose
	@DataProvider
	public String queryUsbkeyByUsername(String username) throws Exception{
		username=ContextHolder.getLoginUser().getUsername();
		return usbkeyDao.queryUsbkeyByUsername(username);
		
	}
	
	@Expose
	public boolean hasUsername(String certId){
		return usbkeyDao.hasCertId(certId);
	}
}
